﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using WJP.ObjectModel;

namespace WJP.MetaModel
{
    /// <summary>
    /// 元数据基类
    /// 支持：SetFieldValue Freezable INotifyPropertyChanged
    /// </summary>
    public abstract class MetaBase : Freezable, INotifyPropertyChanged
    {
        /// <summary>
        /// 设置值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <param name="propertyName"></param>
        protected void SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            CheckUnFrozen();
            if (propertyName != null)
            {
                if (Equals(field, value))
                    return;
                field = value;
                NotifyPropertyChanged(propertyName);
            }
            else
                field = value;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChanged?.Invoke(this, e);
        }

        protected override void CloneValues(IFreezable target, IFreezableCloneOptions option)
        {
            base.CloneValues(target, option);
            this.CopyExtendedProperties(target as MetaBase);
        }
    }
}